package com.aurora.oa.work.view;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.message.adapter.ContactsAdapter;
import com.aurora.oa.message.bean.ContactsDataJsonBean;
import com.aurora.oa.message.bean.ContactsInfo;
import com.aurora.oa.message.presenter.ContactsListActivityPresenter;
import com.aurora.oa.message.presenter.ContactsListActivityPresenterImpl;
import com.aurora.oa.message.view.ContactsListActivityView;
import com.aurora.oa.work.adapter.CompanyStructureAdapter;
import com.aurora.oa.work.bean.CompanyStructureBean;
import com.aurora.oa.work.presenter.CompanyStructureListActivityPresenter;
import com.aurora.oa.work.presenter.CompanyStructureListActivityPresenterImpl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/3/23 0023.
 */
public class CompanyStructureListActivity extends BaseActivity implements CompanyStructureListActivityView{

    RecyclerView rv_contacts_list;
    CompanyStructureAdapter adapter;
    CompanyStructureListActivityPresenter mPresenter;
    List<CompanyStructureBean.ChildNodeBean> contactsInfoList = new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setMyContentView(R.layout.activity_contacts_list);
        initTitlebar(true,"人员选择",true);
        mPresenter = new CompanyStructureListActivityPresenterImpl(this);
        mPresenter.getStructureData();
        rv_contacts_list = (RecyclerView) findViewById(R.id.rv_contacts_list);
        rv_contacts_list.setLayoutManager(new LinearLayoutManager(this));
        rv_contacts_list.addItemDecoration(new ItemDivider().setDividerWith(1).setDividerColor(Color.TRANSPARENT));
        rv_contacts_list.setItemAnimator(new DefaultItemAnimator());

    }

    @Override
    public void showContanctsData(List<CompanyStructureBean> mContactsData) {
        adapter = new CompanyStructureAdapter(this,mContactsData,false);
        rv_contacts_list.setAdapter(adapter);
    }



    @Override
    protected void setResult() {
        String ids="";
        if(adapter!=null){
            Map<String ,Object> contactsAndIDs=adapter.getSelectedContactsAndIDs();
            ids  = (String) contactsAndIDs.get("ids");
            contactsInfoList = (List<CompanyStructureBean.ChildNodeBean>) contactsAndIDs.get("contactsList");
        }
        Intent intent = new Intent();
        intent.putExtra("who_to_send_ids",ids);
        intent.putExtra("contacts_list", (Serializable) contactsInfoList);
        setResult(RESULT_OK,intent);
    }

    @Override
    public void onBackPressed() {
        String ids="";
        if(adapter!=null){
            Map<String ,Object> contactsAndIDs=adapter.getSelectedContactsAndIDs();
            ids  = (String) contactsAndIDs.get("ids");
            contactsInfoList = (List<CompanyStructureBean.ChildNodeBean>) contactsAndIDs.get("contactsList");
        }
        Intent intent = new Intent();
        intent.putExtra("who_to_send_ids",ids);
        intent.putExtra("contacts_list", (Serializable) contactsInfoList);
        setResult(RESULT_OK,intent);
        finish();
    }
}
